package com.zhao.utils;

import com.zhao.common.CommonData;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

import java.util.LinkedHashSet;
import java.util.LinkedList;

/**
 * Description: redis内存数据库操作工具<br/>
 *
 * @author 柒柒
 * @version : 1.0
 * @ClassName JedisClusterUtil <br/>
 * Copyright (c) ，2020 ， 赵 <br/>
 * Date： 2020/12/10 15:42
 */

public class JedisClusterUtil {

    //准备容器,存储JedisCluster池的实例
    private static LinkedList<JedisCluster> pool;

    //对JedisCluster池进行优化
    static {
        pool = new LinkedList<>();

        for (int i = 0; i < 10; i++) {
            JedisCluster instance = createNewJedisCluster();
            pool.add(instance);
        }
    }

    //拆分获取连接redis的端口号
    //redis.host.port=NODE01:7001#NODE01:7002#NODE02:7003#NODE02:7004#NODE03:7005#NODE03:7006
    private static JedisCluster createNewJedisCluster() {
        //Set<HostAndPort> nodes 获取
        String hostAndPortInfos = CommonUtils.getPropertiesValueBykey(CommonData.REDIS_HOST_PORT);
        LinkedHashSet<HostAndPort> nodes = new LinkedHashSet<>();
        String[] arr = hostAndPortInfos.split(CommonData.SPLIT_FLG);
        for (int i = 0; i < arr.length; i++) {
            String[] hostAndPort = arr[i].split(CommonData.SPLIT_FLG2);
            String host = hostAndPort[0].trim();
            int port = Integer.parseInt(hostAndPort[1].trim());
            nodes.add(new HostAndPort(host,port));
        }

        //int timeout 连接超时时间
        int timeout = Integer.parseInt(CommonUtils.getPropertiesValueBykey(CommonData.MAX_WAIT_MILLIS));
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        //最大空闲连接数
        poolConfig.setMaxIdle(Integer.parseInt(CommonUtils.getPropertiesValueBykey(CommonData.MAX_IDLE)));
        //最大连接数
        poolConfig.setMaxTotal(Integer.parseInt(CommonUtils.getPropertiesValueBykey(CommonData.MAX_TOTAL)));
        poolConfig.setMaxWaitMillis(timeout);
        //获取连接是否可用
        poolConfig.setTestOnBorrow(Boolean.valueOf(CommonUtils.getPropertiesValueBykey(CommonData.TEST_ON_BORROW)));

        //maxAttempts
        int maxAttempts = Integer.parseInt(CommonUtils.getPropertiesValueBykey(CommonData.MAX_ATTEMPTS));

        //password
//        String password = CommonUtils.getPropertiesValueBykey(CommonData.PASSWORD);
//
//        return new JedisCluster(nodes,timeout,timeout,maxAttempts,password,poolConfig);
        return new JedisCluster(nodes, timeout, timeout, maxAttempts, poolConfig);
    }

    /**
     * 从JedisCluster池中获得JedisCluster的实例
     * @return
     */
    public static JedisCluster getJedisClusterInstanceFromPool(){
        while (pool.size() == 0){
            CommonUtils.mySleep(1);
        }
        //不是空的就pop,移除
        return pool.pop();
    }

    /**
     * 进行资源释放(伪释放,不是将JedisCluster实例释放,而是重新置于池中供别的线程使用)
     * @param cluster
     */
    public static void resourceRelease(JedisCluster cluster){
        if (cluster != null){
            pool.push(cluster);
        }
    }

}








